package com.carskeeper.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.carskeeper.body.service.BodyUserService;
import com.carskeeper.common.dtos.AppHttpCodeEnum;
import com.carskeeper.common.dtos.PageResponseResult;
import com.carskeeper.common.dtos.ResponseResult;
import com.carskeeper.common.exception.AcException;
import com.carskeeper.mapper.BodyUserMapper;
import com.carskeeper.model.body.dtos.InsertBodyUserDto;
import com.carskeeper.model.body.vo.BodyUserListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Service
@Component
public class BodyUserServiceImpl implements BodyUserService {

    @Autowired
    private BodyUserMapper bodyUserMapper;

    @Override
    public ResponseResult addAdmin(InsertBodyUserDto dto) {
        if (dto==null||dto.getPassword()==null||dto.getUsername()==null||dto.getRealname()==null||dto.getPhone()==null){
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }
        bodyUserMapper.addAdmin(dto);
        return ResponseResult.okResult(null);
    }

    @Override
    public PageResponseResult<List<BodyUserListVo>> findUserList(Long id,Integer pageNum,Integer pageSize) {
        if (id ==null){
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }

        PageHelper.startPage(pageNum,pageSize);
        List<BodyUserListVo> bodyUserListVos = bodyUserMapper.findUserList(id);
        PageInfo<BodyUserListVo> bodyUserListVoPageInfo = new PageInfo<>();
        PageResponseResult<List<BodyUserListVo>> bodyUserListVoPageResponseResult = new PageResponseResult<>(pageNum, pageSize, Math.toIntExact(bodyUserListVoPageInfo.getTotal()));
        bodyUserListVoPageResponseResult.setData(bodyUserListVos);
        return bodyUserListVoPageResponseResult;
    }


    @Override
    public ResponseResult reset(Long id) {

        if (id ==null){
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }
        bodyUserMapper.reset(id);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult lockUser(Long id, Boolean status) {

        if (id==null||status==null){
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }

        bodyUserMapper.lockUser(id,status);
        return ResponseResult.okResult(null);
    }
}
